<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of class
 *
 * @author ww
 */
class StepFourStrategy extends AStepStrategy {

    public function init() {
        $context = $this->getContext();
        $form_manager = $context->getCurrentController()->getFormManager();
        
        if ($typeoftask = $form_manager->getSessionVariable('task_type')) {
            $this->typeoftask = $typeoftask;
        } else {
            $context->setStatus(NOT_FOUND);
            return;
        }
        if (!TaskFormHelper::isAllowedStep(TaskFormHelper::STEP4_FORM_NAME, $typeoftask)) {
            $context->setStatus(NOT_FOUND);
            return;
        }   
    }

    public function getHTML() {
        $context = $this->context;
        $form_manager = $context->getCurrentController()->getFormManager();
        $view = $context->getCurrentController()->getView();
     
        $task_form_factory = new TaskFormFactoryFour();
        $task_form = $task_form_factory->createTaskForm($this->typeoftask); 
        $previous_data = loadDataFromSession(TaskFormHelper::STEP4_FORM_NAME. '_' . $this->typeoftask . '_validation');
        $errors = $form_manager->getErrorsForFields('create_task');      

        $params = array('data' => $previous_data, 'errors' => $errors);
        
        $form_body_html = $task_form->getFormHTML($params, $view);
        $buttons = '<a href="' . HTTP_SERVER . 'task/add/step3' . '">Назад</a><input type="submit" value="Создать задание" name="finish">';
        return $form_manager->build_form($form_body_html, 'create_task', $buttons);
    }

    public function validate_form(&$data, &$scope) {
        $context = $this->getContext();
        $manager = $context->getCurrentController()->getFormManager();
        
        $typeoftask = $manager->getSessionVariable('task_type'); 
        if (isset(TaskFormHelper::$allowedtypes[$typeoftask])) {
            
            $manager->saveDataInToSession($data, TaskFormHelper::STEP4_FORM_NAME . '_' . $typeoftask . '_validation');
            
            $task_form_factory = new TaskFormFactoryFour();
            
            $task_form = $task_form_factory->createTaskForm($typeoftask); 
            $validations_res = $task_form->validate_form($data, $scope, $manager);

            if (in_array(VALIDATION_FAILED, $validations_res)) {
                return VALIDATION_FAILED;
            }
            else
                return VALIDATION_ACCEPTED;
        }
        else {
            Event::fireEvent('message', array('message' => "Попытка взлома?"));
            return VALIDATION_FAILED;
        } 
    }

    public function submit_form($data, $scope) {
        $context = $this->getContext();
        $manager = $context->getCurrentController()->getFormManager();
        $registry = Registry::getInstance();
        $model = new CustomerModel(); 
        
        $type = $manager->getSessionVariable('task_type');
        $step2_data = $manager->loadDataFromSession(TaskFormHelper::STEP2_FORM_NAME . '_' . $type);
        $step3_data = $manager->loadDataFromSession(TaskFormHelper::STEP3_FORM_NAME . '_' . $type);
        $step4_data = &$data;

        $user_id = $registry->user->user_id;
        $is_error = false;
        if (empty($step2_data) || empty($step3_data) || empty($step4_data)) {
            Event::fireEvent('message', array('message' => "Не все этапы пройдены, вернитесь назад"));
            return;
        }
        if (($last_id = $model->add_task($step2_data, $type, $user_id)) !== FALSE) {
            //$overall_data = array_merge_recursive(array_merge_recursive($step2_data, $step3_data), $step4_data);
            switch ($type) {
                case 'reviews':
                case 'voting':
                case 'youtube':
                case 'registrations':
                case 'clicks':
                case 'seo':
                case 'subscriber':
                case 'pcc':
                    $is_error = !$model->add_task_extended($step2_data, $last_id);
                    break;
                case 'twitter':
                case 'surfing':
                case 'test':
                    $is_error = !$model->{"add_task_" . $type}($step2_data, $last_id);
                    break;
                case 'socialnetworks':
                    $is_error = !($model->add_task_extended($step2_data, $last_id) &
                            $model->add_task_socialnetworks($step2_data, $last_id));
                    break;
                default:
                    break;
            }
        } else {
            $is_error = true;
        }
        $manager->clearSessionData();
        if ($is_error) {
            Event::fireEvent('message', array('message' => "Во время добавления записи в БД произошли ошибки"));
            $manager->validation_error[TaskFormHelper::STEP4_FORM_NAME] = true;
        } else {
            Event::fireEvent('message', array('message' => "Задача успешно создана"));
            redirect(DIR_BASE . "task/add");
        }
    }
    

}
?>
